package com.gitee.huanminabc.db_freedom.segments;

import com.gitee.huanminabc.jcommon.str.StringUtil;
import com.gitee.huanminabc.db_freedom.enums.SqlKeyword;
import com.gitee.huanminabc.db_freedom.base.SqlFun;
import com.gitee.huanminabc.db_freedom.wrapper.TableInfo;

import java.util.Collection;

/**
 * 组合实现类
 * @param <T>
 * @param <Children>
 */
public abstract class SqlCombineImplSegments  <T, Children extends CaseWhenImplSegments<T, Children>> extends CaseWhenImplSegments<T, Children> implements SqlCombineSegments<Children, T, SqlFun<T, ?>> {
    public SqlCombineImplSegments() {
    }

    public SqlCombineImplSegments(Class<?> entityClass, com.gitee.huanminabc.db_freedom.wrapper.SQL sql) {
        super(entityClass, sql);
    }

    @Override
    public Children LEFT_JOIN() {
        SQL().setCurrentKeyword(SqlKeyword.LEFT_JOIN);
        SQL().append(" LEFT JOIN ");
        return typedThis;
    }

    @Override
    public Children RIGHT_JOIN() {
        SQL().setCurrentKeyword(SqlKeyword.RIGHT_JOIN);
        SQL().append(" RIGHT JOIN ");
        return typedThis;
    }

    @Override
    public Children INNER_JOIN() {
        SQL().setCurrentKeyword(SqlKeyword.INNER_JOIN);
        SQL().append(" INNER JOIN ");
        return typedThis;
    }

    @Override
    public Children ON() {
        SQL().append(" ON ");
        return typedThis;
    }

    @Override
    public Children UNION() {
        SQL().append(" UNION ").append("\n");
        return typedThis;
    }

    @Override
    public Children UNION_ALL() {
        SQL().append(" UNION ALL ").append("\n");
        return typedThis;
    }
    //使用主表
    public Children TABLE(){
        SQL().append(getTableInfo().getTableName());
        SQL().append(" ");
        SQL().append(getTableInfo().getAlias());
        SQL().append("\n");
        return typedThis;
    }
    //修改主表的别名
    public Children TABLE_ALIAS(String alias){
        //查询别名是否冲突
        Collection<TableInfo> values = getEntityClassList().values();
        for (TableInfo value : values) {
            if(StringUtil.equals(alias,value.getAlias())){
                throw new RuntimeException(alias+"别名冲突,已存在");
            }
        }
        getTableInfo().setAlias(alias);
        TABLE();
        return typedThis;
    }

    //使用指定表
    public Children TABLE(Class<?> entityClass){
        //判断之前是否已经加入过
        TableInfo tableInfo = getEntityClassList().get(entityClass.getName());
        if(tableInfo==null){
            tableInfo = this.createTableInfo(entityClass);
        }
        //sql拼接
        SQL().append(" ").append(tableInfo.getTableName()).append(" ").append(tableInfo.getAlias()).append("\n");
        return typedThis;
    }
    public Children TABLE(Class<?> ...entityClass){
        //不能传空
        if(entityClass==null||entityClass.length==0){
            throw new RuntimeException("TABLE方法参数不能为空");
        }
        for (Class<?> aClass : entityClass) {
            TABLE(aClass);
            SQL().append(",");
        }
        //删除最后一个逗号
        SQL().deleteCharAt(SQL().length()-1);
        SQL().append("\n");
        return typedThis;
    }

    //自定义加表 sql 比如使用子查询啥的
    public Children  TABLE(String sql){
        SQL().append(sql).append("\n");
        return typedThis;
    }
}
